package com.workshop.service.impl;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.base.commom.ResponseUtils;
import com.base.commom.pwd.Md5Utils;
import com.workshop.dao.SysUserDao;
import com.workshop.entity.SysUser;
import com.workshop.service.ISysUserService;
import com.workshop.service.impl.base.BaseServiceImpl;
import com.workshop.utils.ResultMsg;
import com.workshop.utils.session.SessionManager;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chengluqiang
 * @since 2019-04-13
 */
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUserDao, SysUser> implements ISysUserService {
	
	@Resource
	private SysUserDao sysUserDao;
	
	@Override
	public Map<String, Object> loginSubmit(HttpServletRequest request,String username, String password) {
		if (!StringUtils.isNotBlank(username) || !StringUtils.isNotBlank(password)) {
			return ResponseUtils.error("账号或者密码不能为空！", "username or password");
		}
		// 加密
		String pwdMd5 = Md5Utils.getPassword(password, username);
		
		SysUser sysUser = new SysUser();
		sysUser.setUsername(username);
		sysUser = sysUserDao.selectOne(sysUser);
		if(sysUser==null) {
			return ResponseUtils.error("账号不正确！");
		}else {
			if(sysUser.getPassword()!=null&&StringUtils.isNotBlank(sysUser.getPassword())
					&&!sysUser.getPassword().equals(pwdMd5)) {
				return ResponseUtils.error("密码不正确！");
			}else {
				String key = SessionManager.saveUserBySession(request, sysUser);
				return ResponseUtils.success("登录成功！",ResultMsg.USERTOKEN,key);
			}
		}
	}
}
